package com.heartech.prediciton_wx.service.qrcode;

import com.google.gson.Gson;
import com.heartech.prediciton_wx.entity.qrcode.QrStrState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@Slf4j
@Service
public class QrCodeService {

    /* 查询二维码状态 */
    public QrStrState sendGetRequest(String getUrl) {
        QrStrState responseObject = null;

        try {
            URL urlForGetRequest = new URL(getUrl);
            HttpURLConnection con = (HttpURLConnection) urlForGetRequest.openConnection();
            con.setRequestMethod("GET");

            // 添加请求头部
            con.setRequestProperty("Accept", "application/json");

            int responseCode = con.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 打印响应内容以进行调试
                log.info("Response from the server: " + response.toString());

                // 使用Gson解析JSON数据
                Gson gson = new Gson();
                responseObject = gson.fromJson(response.toString(), QrStrState.class);
                con.disconnect();
            } else {
                log.info("GET request not worked");
                con.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return responseObject;
    }


    /* 更改二维码状态 */
    public String sendPostRequest(String postUrl, String postParams) {
        String responseString = null;

        try {
            // 发送 POST 请求来更新状态
            URL urlForPostRequest = new URL(postUrl);
            HttpURLConnection postCon = (HttpURLConnection) urlForPostRequest.openConnection();
            postCon.setRequestMethod("POST");
            postCon.setRequestProperty("Accept", "application/json");
            postCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            postCon.setDoOutput(true);

            OutputStream os = postCon.getOutputStream();
            os.write(postParams.getBytes(StandardCharsets.UTF_8));
            os.flush();
            os.close();

            int postResponseCode = postCon.getResponseCode();
            if (postResponseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader postIn = new BufferedReader(new InputStreamReader(
                        postCon.getInputStream()));
                String postInputLine;
                StringBuilder postResponse = new StringBuilder();
                while ((postInputLine = postIn.readLine()) != null) {
                    postResponse.append(postInputLine);
                }
                postIn.close();
                responseString = postResponse.toString();
                // 打印响应内容以进行调试
                log.info("Response from the server: " + responseString);
                postCon.disconnect();
            } else {
                log.info("POST request not worked");
                postCon.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return responseString; // 或者根据需要返回其他信息
    }
}
